package com.ruoyi.pv.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.doudou.core.container.ListUtil;
import com.doudou.core.thread.ExecutorUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.aliyun.MySms;
import com.ruoyi.pv.domain.*;
import com.ruoyi.pv.service.IPvStudentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pv.mapper.PvAppointMapper;
import com.ruoyi.pv.service.IPvAppointService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 实训预约Service业务层处理
 *
 * @author zzx
 * @date 2023-04-22
 */
@Service
public class PvAppointServiceImpl implements IPvAppointService {

    @Autowired private PvAppointMapper pvAppointMapper;
    @Autowired private IPvStudentRecordService studentRecordService;
//    @Autowired private MySms sms;

    /**
     * 查询实训预约
     *
     * @param id 实训预约主键
     * @return 实训预约
     */
    @Override
    public PvAppoint selectPvAppointById(Long id) {
        return pvAppointMapper.selectPvAppointById(id);
    }

    /**
     * 查询实训预约列表
     *
     * @param pvAppoint 实训预约
     * @return 实训预约
     */
    @Override
    public List<PvAppoint> selectPvAppointList(PvAppoint pvAppoint) {
        return pvAppointMapper.selectPvAppointList(pvAppoint);
    }

    /**
     * 新增实训预约
     *
     * @param pvAppoint 实训预约
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPvAppoint(PvAppoint pvAppoint) {
        pvAppoint.setCreateTime(DateUtils.getNowDate());
        int i = pvAppointMapper.insertPvAppoint(pvAppoint);
        insertPvAppointTime(pvAppoint);
        return i;
    }

    /**
     * 修改实训预约
     *
     * @param pvAppoint 实训预约
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePvAppoint(PvAppoint pvAppoint) {
        pvAppointMapper.deletePvAppointTimeByAppointId(pvAppoint.getId());
        insertPvAppointTime(pvAppoint);
        return pvAppointMapper.updatePvAppoint(pvAppoint);
    }

    /**
     * 批量删除实训预约
     *
     * @param ids 需要删除的实训预约主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePvAppointByIds(Long[] ids) {
        pvAppointMapper.deletePvAppointTimeByAppointIds(ids);
        return pvAppointMapper.deletePvAppointByIds(ids);
    }

    /**
     * 删除实训预约信息
     *
     * @param id 实训预约主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePvAppointById(Long id) {
        pvAppointMapper.deletePvAppointTimeByAppointId(id);
        return pvAppointMapper.deletePvAppointById(id);
    }

    /**
     * 新增预约时段表信息
     *
     * @param pvAppoint 实训预约对象
     */
    public void insertPvAppointTime(PvAppoint pvAppoint) {
        List<PvAppointTime> pvAppointTimeList = pvAppoint.getPvAppointTimeList();
        Long id = pvAppoint.getId();
        if (ListUtil.isNotEmpty(pvAppointTimeList)) {
            pvAppointMapper.batchPvAppointTime(pvAppointTimeList.stream().peek(l->l.setAppointId(id)).collect(Collectors.toList()));
        }
    }

    /**
     * 发送通知预约短信
     */
    @Override
    public void smsSend(Long id,String time,List<Long> classIds) {
        //查询预约详情
        PvAppoint appoint = pvAppointMapper.selectPvAppointById(id);
        //查询用户
        Set<SysUser> users = getSmsUsers(classIds);
        //创建线程池
        ThreadPoolExecutor executor = ExecutorUtil.customThreadExecutor(4, 4, 0L, users.size());
        //发送短信
//        for (SysUser user : users) {
//            executor.execute(() -> {
//                if (StrUtil.isNotBlank(user.getPostsNumber())) {
//                    sms.sendFirstAppoint(user.getPhonenumber(), user.getNickName(),
//                            DateUtil.thisYear(), DateUtil.formatDate(appoint.getBeginDate()), DateUtil.formatDate(appoint.getEndDate()), time);
//                }
//            });
//        }
        //修改状态
        appoint.setReservedTwo("1");
        pvAppointMapper.updatePvAppoint(appoint);
    }

    @Override
    public int smsSendPeopleCount(List<Long> classId) {
        return getSmsUsers(classId).size();
    }

    /**
     * 查询可以预约的场地
     */
    @Override
    public List<PvRoom> selectPvAppointRoomList() {
        return pvAppointMapper.selectPvAppointRoomList();
    }


    /**
     * 查询发送短信的用户
     */
    private Set<SysUser> getSmsUsers(List<Long> classIds) {
        Set<SysUser> users = new HashSet<>();
        List<PvStudentRecord> pvStudentRecords;
        //查询用户
        for (Long classId : classIds) {
            pvStudentRecords = studentRecordService.selectPvTrainingClassStudentList(classId, new SysUser());
            users.addAll(parseUsers(pvStudentRecords));
        }
        return users;
    }

    private Set<SysUser> parseUsers(List<PvStudentRecord> pvStudentRecords) {
        return pvStudentRecords.stream().map(PvStudentRecord::getStudent).collect(Collectors.toSet());
    }
}
